<%@page import="java.security.Principal"%>
<%@page import="java.util.List"%>
<%@page import="it.trekker.upload.UploadUtilities"%>
<%@page import="it.trekker.upload.FileLoaderFactory"%>
<%@page import="it.trekker.upload.IFileLoader2Database"%>
<%@page import="it.trekker.upload.json.IFileLoaderFactory"%>

<%
Principal principal = request.getUserPrincipal();
String userName;
if(principal != null)
	userName = request.getUserPrincipal().getName();
else
	userName = "";

IFileLoaderFactory fileLoaderFactory = new FileLoaderFactory();
IFileLoader2Database fileLoader = fileLoaderFactory.getFileLoader();

List<String> pathList = UploadUtilities.receiveAndSaveFile(request, getServletContext().getRealPath("/"));
for(String filePath : pathList) {
	fileLoader.extractData2Database(filePath);
}
%>

<html>
	<head>
		<title>Trekker</title>
	
		<meta http-equiv="Pragma" content="no-cache"/>
		<meta http-equiv="Expires" content="Mon, 01 Jan 1996 23:59:59 GMT"/>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta name="Author" content="you">

		<link rel="StyleSheet" href="styles/default.css" type="text/css" media="all" />
		
		
  		<script type="text/javascript">
        	function fileSelected() {
            	var file = document.getElementById('fileToUpload').files[0];
            	
            	if (file) {
	            	if(file.type != "application/json")
	            		alert("ERROR: Invalid file type")
	            	else {
			                var fileSize = 0;
			                if (file.size > 1024 * 1024)
			                    fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
			                else
			                    fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
			
			                document.getElementById('fileName').innerHTML = '<b>Name:</b> ' + file.name;
			                document.getElementById('fileSize').innerHTML = '<b>Size:</b> ' + fileSize;
			                document.getElementById('fileType').innerHTML = '<b>Type:</b> ' + file.type;
			                
			                document.getElementById('progressNumber').style.visibility = "hidden";
			                document.getElementById('prog').style.visibility = "hidden";
			                document.getElementById('prog').value = 0;
			                document.getElementById('uploadFileBt').style.visibility = "visible";
			        }
            	}
	        }
	
	        function uploadFile() {
                document.getElementById('progressNumber').style.visibility = "visible";
                document.getElementById('prog').style.visibility = "visible";
                
	            var fd = new FormData();
	            fd.append("fileUp", document.getElementById('fileToUpload').files[0]);
	            var xhr = new XMLHttpRequest();
	            xhr.upload.addEventListener("progress", uploadProgress, false);
	            xhr.addEventListener("load", uploadComplete, false);
	            xhr.addEventListener("error", uploadFailed, false);
	            xhr.addEventListener("abort", uploadCanceled, false);
	            xhr.open("POST", "rest/upload/json");
	            xhr.send(fd);
	        }
	
	        function uploadProgress(evt) {
	            if (evt.lengthComputable) {
	                var percentComplete = Math.round(evt.loaded * 100 / evt.total);
	                document.getElementById('progressNumber').innerHTML = percentComplete.toString() + '%';
	                document.getElementById('prog').value = percentComplete;
	            }
	            else {
	                document.getElementById('progressNumber').innerHTML = 'unable to compute';
	            }
	        }
	
	        function uploadComplete(evt) {
	            /* This event is raised when the server send back a response */
	            alert(evt.target.responseText);
	        }
	
	        function uploadFailed(evt) {
	            alert("There was an error attempting to upload the file.");
	        }
	
	        function uploadCanceled(evt) {
	            alert("The upload has been canceled by the user or the browser dropped the connection.");
	        }
	    </script>
	</head>
	
	<body>
		<div id="menu_link">
			<a href="logout.jsp">Logout</a>
		</div>
		
		<h1>Trekker</h1>
		
		<h2>Welcome <%= userName %></h2>
		
		<div id="upload_file">
			Upload a Json file: 
			<form class="fileUpload">
				<input type='file' name='fileUp' id="fileToUpload" onchange="fileSelected();">
				<div class=fileDetails>
					<div class="fileName" id="fileName"></div>
				    <div class="fileSize" id="fileSize"></div>
				    <div class="fileType" id="fileType"></div>
					<input type='button' id="uploadFileBt" value='Upload File' onclick="uploadFile();" style="visibility:hidden">
		    		<progress class="prog" id="prog" value="0" max="100.0" style="visibility:hidden"></progress>
				    <div class="progressNumber" id="progressNumber" style="visibility:hidden"></div>
	    		</div>
			</form>
		</div>
		
		<div>
			<label><a href="users.jsp">Users list</a></label>
		</div>
		<div>
			<label><a href="search.jsp">Search excursions page</a></label>
		</div>
		<div>
			<label><a href="users">Manage your excursions</a></label>
		</div>
	</body>

</html>
